package com.aegean.CommercialHotelBookSystem.hotel.business.Impl;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.List;

import org.hibernate.Hibernate;

import com.aegean.CommercialHotelBookSystem.factory.DaoFactory;
import com.aegean.CommercialHotelBookSystem.hotel.business.IHotelBusiness;
import com.aegean.CommercialHotelBookSystem.hotel.dao.IHotelDao;
import com.aegean.CommercialHotelBookSystem.hotel.dao.Impl.HotelDaoImpl;
import com.aegean.CommercialHotelBookSystem.model.Customer;
import com.aegean.CommercialHotelBookSystem.model.Hotel;
import com.aegean.CommercialHotelBookSystem.model.component.Address;
import com.aegean.CommercialHotelBookSystem.model.component.Photo;
import com.aegean.CommercialHotelBookSystem.user.dao.Impl.UserDaoImpl;

public class HotelBusinessImpl implements IHotelBusiness {
	static int searchHotelCount;

	public boolean searchHotel(String hotelName) {
		Hotel one = null;
		IHotelDao hoteldao = DaoFactory.createHotelDao();
		one = hoteldao.searchHotel(hotelName);
		if (one == null) {
			return false;
		} else {
			return true;
		}
	}

	@Override
	public boolean saveHotel(String hotelName, String hotelComments,
			String fileName, String fileType, String country, String city,
			String community) {
		Hotel one = new Hotel();
		Photo photo = new Photo();
		Address address = new Address();
		try {
			photo.setPhoto_Name(fileName);
			photo.setPhoto_Type(fileType);
			address.setCountry(country);
			address.setCity(city);
			address.setCommunity(community);
			one.setHotel_Name(hotelName);
			one.setHotel_Comments(hotelComments);
			one.setHotel_Address(address);
			one.setHotel_Photo(photo);
			IHotelDao hoteldao = DaoFactory.createHotelDao();
			hoteldao.saveHotel(one);
			return true;
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
	}

	@Override
	public List<Hotel> searchHotel(int intpage, int intpagesize, String name,
			String city) {
		IHotelDao hoteldao = DaoFactory.createHotelDao();
		List<Hotel> hotels = hoteldao.searchHotelList(intpage, intpagesize,
				name, city);
		return hotels;
	}

	@Override
	public int searchHotelCount(String name, String city, int intpagesize) {
		IHotelDao hoteldao = DaoFactory.createHotelDao();
		int introwcount = hoteldao.searchHotelListCount(name, city);
		int intpagecount = (introwcount + intpagesize - 1) / intpagesize;
		return intpagecount;
	}

	@Override
	public boolean editHotel(Hotel one,
			String hotelComments, String country, String city,
			String community, String fileName, String fileType) {
		Photo photo = null;
		Address address = null;
		try {
			if (fileName != null) {
				photo = new Photo();
				photo.setPhoto_Name(fileName);
				photo.setPhoto_Type(fileType);
				one.setHotel_Photo(photo);
			}
			address = new Address();
			address.setCountry(country);
			address.setCity(city);
			address.setCommunity(community);
			one.setHotel_Comments(hotelComments);
			one.setHotel_Address(address);
			IHotelDao hoteldao = DaoFactory.createHotelDao();
			hoteldao.editHotel(one);
			return true;
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
	}
}
